package com.good.msgsys.service.impl;

import com.good.msgsys.dao.UserDao;
import com.good.msgsys.dto.PageResult;
import com.good.msgsys.dto.ResultCode;
import com.good.msgsys.entity.User;
import com.good.msgsys.entity.request.ChangePwdVo;
import com.good.msgsys.entity.response.UserInfo;
import com.good.msgsys.exception.CommonException;
import com.good.msgsys.service.UserService;
import com.good.msgsys.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author junlong
 * @date 2020/12/31 11:08
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(User user) {
        // 生成id
        String id = idWorker.nextId() + "";
        user.setId(id);
        userDao.save(user);
    }

    @Override
    public void deleteById(String id) {
        // 逻辑删除
        User temp = userDao.findById(id).get();
        temp.setState(0); // 删除用户：将用户的状态改为 0 - 已删除
        userDao.save(temp);
    }

    @Override
    public User update(User user) {
        User temp = userDao.findById(user.getId()).get();
        if (user.getNum() != null) temp.setNum(user.getNum());
        if (user.getName() != null) temp.setName(user.getName());
        if (user.getJob() != null) temp.setJob(user.getJob());
        if (user.getSchool() != null) temp.setSchool(user.getSchool());
        if (user.getJoinTime() != null) temp.setJoinTime(user.getJoinTime());
        if (user.getBirthday() != null) temp.setBirthday(user.getBirthday());
        if (user.getPhone() != null) temp.setPhone(user.getPhone());
        if (user.getPolitical() != null) temp.setPolitical(user.getPolitical());
        if (user.getGender() != null) temp.setGender(user.getGender());
        if (user.getDegree() != null) temp.setDegree(user.getDegree());
        if (user.getEmail() != null) temp.setEmail(user.getEmail());
        if (user.getEducation() != null) temp.setEducation(user.getEducation());
        if (user.getAddress() != null) temp.setAddress(user.getAddress());
        return userDao.save(temp);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id).get();
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public PageResult<UserInfo> findAll(Integer state, Integer page, Integer limit) {
        Pageable pageable = PageRequest.of(page, limit);
        Page<UserInfo> userPage = userDao.findAllUser(0, state, pageable);
        return new PageResult<>(userPage.getTotalElements(), userPage.getContent(), (long) userPage.getTotalPages());
    }

    @Override
    public User findByAccount(String account) {
        return userDao.findByAccount(account);
    }

    @Override
    public void resetPwd(String id) {
        User user = userDao.findById(id).get();
        user.setPassword("666666");
        userDao.save(user);
    }

    @Override
    public void changePwd(ChangePwdVo changePwdVo) throws CommonException {
        String id = changePwdVo.getId();
        String newPwd = changePwdVo.getNewPwd();
        String oldPwd = changePwdVo.getOldPwd();
        if (!StringUtils.isEmpty(id) && !StringUtils.isEmpty(newPwd) && !StringUtils.isEmpty(oldPwd)) {
            User user = userDao.findById(id).get();
            String tempPwd = user.getPassword();
            if (oldPwd.equals(tempPwd)) {
                user.setPassword(newPwd);
                userDao.save(user);
            }else {
                throw new CommonException(ResultCode.PWD_ERROR);
            }
        }else {
            throw new CommonException(ResultCode.PARAM_ERROR);
        }
    }
}
